local base = _G
module("GUI.EventHandler.Form.Main")

local os = base.os.getenv("OS")
if os == "Windows_NT" then
	rootdir = ".\\"
else
	rootdir = "\\Program Files\\wmssystem\\"
end
base.print("DBG:Eventhandler GUI.EventHandler.Form.Main Loaded.\n")
function Parse(methon,para)
	base.print("DBG: methon: " .. methon .. " para: " .. para .. "\n")
	if(methon == "pendingorder") then
		pendingorder()
	elseif(methon == "stock") then
		stock()
	elseif(methon == "order") then
		order()
	elseif(methon == "voice") then
		voice()
	elseif(methon == "exit") then
		exit()
	else
		base.print("DBG:Unknown action\n")
	end
	base.os.exit(0)
end

function pendingorder()
	local file = base.io.open(rootdir .. "\\tmp\\form.txt","w")
	file:write("pendingorder")
	file:close()
	
	local f = base.require("Function.order_management")
	local content = f.QueryForPendingOrder()
	
	local h_xmlfile = base.io.open(rootdir .. "XML\\pendingorder.xml","rb")
	xmlfile = h_xmlfile:read("*all")
	h_xmlfile:close()
		
	xmlfile = base.string.gsub(xmlfile,"{Order1}",content[1] or "Unavailable");
	xmlfile = base.string.gsub(xmlfile,"{Order2}",content[2] or "Unavailable");
	xmlfile = base.string.gsub(xmlfile,"{Order3}",content[3] or "Unavailable");
	xmlfile = base.string.gsub(xmlfile,"{Order4}",content[4] or "Unavailable");
	xmlfile = base.string.gsub(xmlfile,"{Order5}",content[5] or "Unavailable");
	
	local h_xmlfile = base.io.open(rootdir .. "XML\\tmp\\pendingorder.xml","wb")
	h_xmlfile:write(xmlfile)
	h_xmlfile:close()
end
function order()
	local file = base.io.open(rootdir .. "\\tmp\\form.txt","w")
	file:write("order")
	file:close()
end
function voice()
	local file = base.io.open(rootdir .. "\\tmp\\form.txt","w")
	file:write("voice")
	file:close()
end
function stock()
	local file = base.io.open(rootdir .. "\\tmp\\form.txt","w")
	file:write("stock")
	file:close()
end
function exit()
	local file = base.io.open(rootdir .. "\\tmp\\exit.txt","w")
	file:write("exit")
	file:close()
end